package cn.javadog.smartbreeding.record.util;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 流水号
 * @author 余勇
 */
public class SerialNoUtil {

	private static final HashMap<String, LinkedBlockingQueue<String>> billBizOrderMap = new HashMap<>();

	/**
	 * 生成单据流水号
	 */
	public static String newBillSerialNo(String channel, LocalDate saleDate) {
		StringBuilder sb = new StringBuilder();
		String dateFormat = saleDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
		sb.append(dateFormat)
			.append(channel)
			.append(getBillBizOrder(channel, dateFormat));
		return sb.toString();
	}

	private static String getBillBizOrder(String channel, String date) {
		String key = date + channel;
		if (!billBizOrderMap.containsKey(key)){
			LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>(100);
			for (int i = 1; i < 100; i++) {
				if (i < 10) {
					queue.offer("00" + i);
				} else if (i < 100) {
					queue.offer("0" + i);
				} else {
					queue.offer(String.valueOf(i));
				}
			}
			billBizOrderMap.put(key, queue);
		}
		return billBizOrderMap.get(key).poll();
	}

}
